package com.learning.jquizenglish.widget.metro;

import java.util.ArrayList;
import java.util.Iterator;

import android.content.Context;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.widget.LinearLayout;
import android.widget.ScrollView;

/**
 * The Class MetroLayoutPortrait.
 * @author HuanND
 */
public class MetroLayoutPortrait extends ScrollView implements Metro{

	/**
	 * Instantiates a new metro layout portrait.
	 *
	 * @param context the context
	 */
	public MetroLayoutPortrait(Context context) {
		super(context);
		setVerticalScrollBarEnabled(true);
		setFadingEdgeLength(0);
		setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View view, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_UP) {
					MetroLayoutPortrait.this.resetAlpha("inside");
				}
				return false;
			}
		});
	}

	
	/**
	 * Gets the view by tag.
	 *
	 * @param vg the vg
	 * @param tag the tag
	 * @return the view by tag
	 */
	private ArrayList<View> getViewByTag(ViewGroup vg,String tag){
		ArrayList<View> arrView = new ArrayList<View>();
		int childCount = vg.getChildCount();
		for (int i = 0; i < childCount; i++) {
			View v = vg.getChildAt(i);
			if (v instanceof ViewGroup) {
				arrView.addAll(getViewByTag((ViewGroup) v, tag));
			}
			Object ob = v.getTag();
			if ( ob == null || !ob.equals(tag)) {
				continue;
			}
			arrView.add(v);
		}
		return arrView;
	}
	
	/* (non-Javadoc)
	 * @see com.learning.jquizenglish.widget.metro.Metro#resetAlpha(java.lang.String)
	 */
	@SuppressWarnings("rawtypes")
	@Override
	public void resetAlpha(String tag) {
		Iterator iterator = getViewByTag(this, tag).iterator();
		while (true) {
			if (!iterator.hasNext()) {
				return;
			}
			Object ob = iterator.next();
			AlphaAnimation alphaAnimation = new AlphaAnimation(1.0F, 1.0F);
			alphaAnimation.setDuration(0L);
			alphaAnimation.setFillAfter(true);
			((LinearLayout)ob).startAnimation(alphaAnimation);
		}
	}

}
